package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.PlanReqPara;
import com.lst.entity.response.Plan2ResPara;
import com.lst.model.Plan;
import com.lst.model.PlanCourse;
import com.lst.model.SportRecords;
import com.lst.service.PlanCourseService;
import com.lst.service.PlanService;
import com.lst.service.SportRecordsService;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: PlanDetailServlet
 * @Description: 我的功夫计划详情接口
 * 
 */
public class PlanDetailServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private PlanCourseService planCourseService = ctx.getBean(PlanCourseService.class);
	
	private PlanService planService = ctx.getBean(PlanService.class);
	
	private SportRecordsService sportRecordsService = ctx.getBean(SportRecordsService.class);
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Plan2ResPara resPara = new Plan2ResPara();

		try {
			PlanReqPara reqPara = getReqPara(request);
		
		if(isCheckField(reqPara,resPara)){
     		Integer userid = Integer.valueOf(reqPara.getUserid());
     		Integer planId = Integer.valueOf(reqPara.getPlanId());
     		
     		Map<String,Object> reMap2=new HashMap<String, Object>();
			reMap2.put("createUser", userid);
			reMap2.put("planId", planId);
			reMap2.put("orderByClause", "md.trainDate");
			List<PlanCourse> planCourseList=planCourseService.selectByUser2(reMap2);
			
			if(planCourseList!=null && planCourseList.size()>0){
				for(PlanCourse course:planCourseList){
					  Date date=course.getTrainDate();
 					  Long trainDate2=date.getTime();
 					  course.setTrainDate2(trainDate2);
				}
			}
			Plan plan=planService.selectById(planId);
			if(plan!=null){
				resPara.setIsNotice(plan.getIsNotice());
				resPara.setNoticeTime(plan.getNoticeTime());
			}else{
				resPara.setIsNotice(false);
				resPara.setNoticeTime("07:00");
			}
			
			Map<String,Object> reMap=new HashMap<String, Object>();
			reMap.put("planId", planId);
			List<SportRecords> sportRecordsList=sportRecordsService.queryByList(reMap);
			int totalTime=0;
			if(sportRecordsList!=null&&sportRecordsList.size()>0){
				for(SportRecords sportRecords:sportRecordsList){
					totalTime=totalTime+sportRecords.getTrainTime();
				}
			}
				
			resPara.setTotalTime(totalTime);
			
			resPara.setPlanCourseList(planCourseList);
			resPara.setCode(CommCode.M_Y000000);
			resPara.setMessage(CommCode.M_Y000001);
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
	//		logger.info("PlanDetailServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

	//		logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public PlanReqPara getReqPara(HttpServletRequest request) {

		PlanReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new PlanReqPara());

			reqPara = (PlanReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String planId = request.getParameter("planId");
			
//			logger.info("reqPara userid:" + userid);
//			logger.info("reqPara planId:" + planId);
		
			reqPara.setPlanId(planId);
	        reqPara.setUserid(userid);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		PlanReqPara cdReqPara = (PlanReqPara) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

       if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		} else if (StringUtils.isEmpty(cdReqPara.getPlanId())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("计划id为空");
		}  else {
			flag = true;
		}

		return  flag;
	}

}
